package com.gan.springbootdemo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

@Slf4j
public class VideoFileDownload {

    public static void main(String[] args) {
        String authToken = "53c6da20-0a4c-11e8-b239-00163e041f45";
        String fileUrl = "http://101.132.152.150:18081/openapi/playback/file";
        String params = "{ \"device_serial\": \"FH00185H00\", \"chan_no\": 1, \"url\": \"http://220.160.111.78:8604/video/video-upload\"," +
                "\"st_time\": 1532396400, \"en_time\": 1532396460 }";
        String responseBody = HttpUtil.post(fileUrl, params);
        log.info(responseBody);
        JSONObject obj = (JSONObject) JSON.parse(responseBody);
        final String downloadUrl = obj.getString("url");
        log.info(downloadUrl);
//        String downloadUrl = "http://101.132.152.150:18081/icloud/store/video-1000005559001-1532414237-1532396400-1532396460?e=1532417837&t=icloud:OPImDRP6N3jLMlWWpo6f_X-coWE=";
        try {
            Thread.sleep(1000 * 20);
        } catch (InterruptedException e) {
            log.error("InterruptedException", e);
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                download(downloadUrl);
            }
        }).start();
    }

    private static void download(String urlStr) {
        HttpURLConnection httpURLConnection;
        try {
            final URL url = new URL(urlStr);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            // 设置关键值
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(20 * 1000);
            httpURLConnection.setReadTimeout(20 * 1000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setUseCaches(false);
            // 设置请求头信息
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("Accept-Language", "zh-CN");
            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.connect();
            // 读取响应码
            log.info("getResponseCode: " + httpURLConnection.getResponseCode());
            log.info("getResponseMessage: " + httpURLConnection.getResponseMessage());
            if (httpURLConnection.getResponseCode() == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();
                FileOutputStream fos = new FileOutputStream(new File("D:/home/video"));
                byte[] buffer = new byte[10240];
                int length;
                while ((length = inputStream.read(buffer)) >= 0) {
                    log.info("length: {} Byte", length);
                    fos.write(buffer, 0, length);
                }
                fos.close();
                inputStream.close();
                httpURLConnection.disconnect();
            }
        } catch (IOException e) {
            log.error("IOException", e);
        }
    }

}